package hz.mobileproject.pda.event;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;

import hz.mobileproject.pda.listener.NetInterFace;


/**
 * Created by wangliang on 2016/11/23.
 */

public class NetBroadReceiver extends BroadcastReceiver {

    NetInterFace face;
    private NetworkInfo mobileInfo;
    private NetworkInfo wifiInfo;
    private NetworkInfo activeInfo;

    public NetBroadReceiver(NetInterFace face){
        this.face=face;

    }


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        //Toast.makeText(context, intent.getAction(), 1).show();
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);


        try {
             mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
             wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
             activeInfo = manager.getActiveNetworkInfo();

//            Toast.makeText(context, "wifi:"+wifiInfo.isConnected()
//                    +"\n"+"active:"+activeInfo.getTypeName(), Toast.LENGTH_SHORT).show();

            if (activeInfo==null){
                face.stace(1);
                Toast.makeText(context,"网络不可以用",Toast.LENGTH_SHORT).show();

            }else {
                //改变背景或者 处理网络的全局变量
//                Toast.makeText(context,"网络可以用",Toast.LENGTH_SHORT).show();
                face.stace(0);
            }

        }catch (Exception e){

            LogUtils.e("网络不可以用");
            face.stace(1);
        }





    }  //如果无网络连接activeInfo为null





}